package com.example.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.commonsutils.R;
import com.example.eduservice.entity.EduTeacher;
import com.example.eduservice.entity.vo.EduTeacherPageVO;
import com.example.eduservice.mapper.EduTeacherMapper;
import com.example.eduservice.service.EduTeacherService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:tjf
 * @Description:
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService{
    @Resource
    private EduTeacherMapper teacherMapper;

    /**
     * 条件查询教师带分页
     * @param current
     * @param limit
     * @param pageVO
     * @return
     */
    @Override
    public R findTeacherByConditionForPage(Integer current, Integer limit, EduTeacherPageVO pageVO) {
        if (current == null || current < 0){
            // 默认为第一页
            current = 1;
        }
        if (limit == null || limit < 0){
            // 默认每页记录数为5
            limit = 5;
        }
        // 创建分页对象
        Page<EduTeacher> eduTeacherPage = new Page<>(current, limit);

        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper();

        String name = pageVO.getName();
        Integer level = pageVO.getLevel();
        String begin = pageVO.getBegin();

        // 判断条件字段是否为空，若不为空，则拼接到条件构造器中
        if (StringUtils.isNotBlank(name)){
            queryWrapper.like(EduTeacher::getName, pageVO.getName());
        }
        if (level != null && level > 0){
            queryWrapper.eq(EduTeacher::getLevel, pageVO.getLevel());
        }
        if (StringUtils.isNotBlank(begin)){
            queryWrapper.ge(EduTeacher::getGmtCreate, begin);
        }
        queryWrapper.orderByDesc(EduTeacher::getGmtCreate);
        // 执行查询方法
        Page<EduTeacher> result  = teacherMapper.selectPage(eduTeacherPage, queryWrapper);

        return R.ok().data("total", result.getTotal()).data("teachers", result.getRecords());
    }

    /**
     * 查询名师
     * @return
     */
    @Cacheable(cacheNames = "eduservice::front", key = "'popularTeachers'")
    @Override
    public List<EduTeacher> queryPopularTeachers() {
        LambdaQueryWrapper<EduTeacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(EduTeacher::getId);
        Page<EduTeacher> page = teacherMapper.selectPage(new Page<EduTeacher>(1, 4), wrapper);
        if (page != null){
            return page.getRecords();
        }
        return null;
    }

    /**
     * 分页查询教师列表
     *
     * @param eduTeacherPage
     * @return
     */
    @Override
    public HashMap<String, Object> queryTeacherListForLimit(Page<EduTeacher> eduTeacherPage) {
        LambdaQueryWrapper<EduTeacher> wrapper = new LambdaQueryWrapper();
        wrapper.orderByDesc(EduTeacher::getGmtCreate);

        // 分页查询教师
        teacherMapper.selectPage(eduTeacherPage, wrapper);

        // 封装返回结果
        List<EduTeacher> records = eduTeacherPage.getRecords();
        long current = eduTeacherPage.getCurrent();
        long pages = eduTeacherPage.getPages();
        long size = eduTeacherPage.getSize();
        long total = eduTeacherPage.getTotal();
        boolean hasNext = eduTeacherPage.hasNext();//下一页
        boolean hasPrevious = eduTeacherPage.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        HashMap<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
